package com.future.hedongjun.mine;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.CardView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.future.baselib.entity.BaseResponse;
import com.future.baselib.http.HttpRequest;
import com.future.baselib.utils.TxtUtils;
import com.future.baselib.view.CircleImageView;
import com.future.hedongjun.R;
import com.future.hedongjun.common.MainActivity;
import com.future.hedongjun.common.MyApp;
import com.future.hedongjun.mall.activity.CouponListActivity;
import com.future.hedongjun.mine.bean.UserInfo;
import com.future.hedongjun.mine.bean.response.UserInfoResponse;
import com.future.hedongjun.mine.ui.ClassCacheActivity;
import com.future.hedongjun.mine.ui.DeliveryAddressActivity;
import com.future.hedongjun.mine.ui.MyClassActivity;
import com.future.hedongjun.mine.ui.MyOrderActivity;
import com.future.hedongjun.mine.ui.ReportActivity;
import com.future.hedongjun.mine.ui.SettingActivity;
import com.future.hedongjun.mine.ui.UserInfoActivity;
import com.jaeger.library.StatusBarUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import jp.wasabeef.glide.transformations.BlurTransformation;

/**
 * A simple {@link Fragment} subclass.
 */
public class MineFragment extends Fragment {

    public static final int REQUEST_CODE_LOGOUT = 0x123;

    @BindView(R.id.iv_icon_bg)
    ImageView ivIconBg;
    @BindView(R.id.layout_person)
    View layoutPerson;
    @BindView(R.id.iv_icon)
    CircleImageView ivIcon;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.layout_person_info)
    RelativeLayout layoutPersonInfo;
    @BindView(R.id.layout_my_class)
    TextView layoutMyClass;
    @BindView(R.id.layout_class_cache)
    TextView layoutClassCache;
    @BindView(R.id.layout_my_order)
    TextView layoutMyOrder;
    @BindView(R.id.layout_my_youhuiiquan)
    TextView layoutMyYouhuiiquan;
    @BindView(R.id.cv_my)
    CardView cvMy;
    @BindView(R.id.layout_address)
    TextView layoutAddress;
    @BindView(R.id.layout_report)
    TextView layoutReport;
    @BindView(R.id.layout_setting)
    TextView layoutSetting;
    Unbinder unbinder;


    public MineFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mine, container, false);
        unbinder = ButterKnife.bind(this, view);
        StatusBarUtil.setTranslucentForImageViewInFragment(getActivity(), 0x30, null);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        updateUserView();
    }

    private void updateUserView() {
        UserInfo info = MyApp.getInstance().getUserInfo();
        Glide.with(this)
                .load(info.getAvatar())
                .apply(RequestOptions.placeholderOf(R.drawable.tab_wd_mr).dontAnimate())
                .into(ivIcon);
        Glide.with(this)
                .load(info.getAvatar())
                .apply(RequestOptions
                        .bitmapTransform(new BlurTransformation(10))
                        .placeholderOf(R.color.trans)
                        .dontAnimate())
                .into(ivIconBg);
        tvName.setText(TextUtils.isEmpty(info.getNickname()) ? TxtUtils.hidePhoneMiddle(info.getMobile()) : info.getNickname());
        tvPhone.setText(TxtUtils.hidePhoneMiddle(info.getMobile()));
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.layout_person_info, R.id.layout_my_class, R.id.layout_class_cache, R.id.layout_my_order, R.id.layout_my_youhuiiquan, R.id.layout_address, R.id.layout_report, R.id.layout_setting})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.layout_person_info:
                startActivityForResult(new Intent(getContext(), UserInfoActivity.class), REQUEST_CODE_LOGOUT);
                break;
            case R.id.layout_my_class:
                startActivity(new Intent(getContext(), MyClassActivity.class));
                break;
            case R.id.layout_class_cache:
                startActivity(new Intent(getContext(), ClassCacheActivity.class));
                break;
            case R.id.layout_my_order:
                startActivity(new Intent(getContext(), MyOrderActivity.class));
                break;
            case R.id.layout_my_youhuiiquan:
                startActivity(new Intent(getContext(), CouponListActivity.class));
                break;
            case R.id.layout_address:
                startActivity(new Intent(getContext(), DeliveryAddressActivity.class));
                break;
            case R.id.layout_report:
                startActivity(new Intent(getContext(), ReportActivity.class));
                break;
            case R.id.layout_setting:
                startActivity(new Intent(getContext(), SettingActivity.class));
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_CODE_LOGOUT) {
            ((MainActivity) getActivity()).controller.setSelect(0);
        }
    }
}
